package com.gdth.bank.activity;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.gdth.bank.BankApplication;
import com.gdth.bank.R;
import com.gdth.bank.adapter.PhoneGridAdapter;
import com.gdth.bank.presenter.DzCpKhXxEtcPresenter;
import com.gdth.bank.presenter.DzCpKhXxFjPresenter;
import com.gdth.bank.utils.AppUtil;
import com.gdth.bank.utils.MobileCheck;
import com.gdth.bank.utils.ServerUtil;
import com.gdth.bank.view.DzCpKhXxEtcView;
import com.gdth.bank.widget.BankDrawDownListTextView;
import com.gdth.bank.widget.BankEditText;
import com.gdth.bank.widget.PopupWindows;
import com.gdth.bank.widget.image.Bimp;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseActivity;
import com.gdth.database.bean.DzCpkhXxDjEtc;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.database.bean.TAppYh;
import com.gdth.database.bean.TXtBm;
import com.gdth.model.Item;
import com.gdth.utils.RegularUtil;
import com.gdth.utils.ToolUtil;
import com.gdth.utils.UUIDUtil;
import com.gdth.widget.dialog.BaseToast;

public class DzCpKhXxEtcActivity extends BaseActivity  implements DzCpKhXxEtcView{
	@ViewInject(id=R.id.header_label_title) TextView txt_header_title;
	@ViewInject(id=R.id.header_btn_left, click="finishClick") TextView txt_header_left;
	@ViewInject(id=R.id.header_btn_right, click="saveClick") TextView photo_bt_del;
	@ViewInject(id=R.id.dzcpkh_xm) BankEditText txt_khxm;
	@ViewInject(id=R.id.dzcpkh_sfzhm) BankEditText txt_khsfzhm;
	@ViewInject(id=R.id.dzcpkh_sjhm) BankEditText txt_khsjhm;
	@ViewInject(id=R.id.dzcpkh_etc_jg) BankDrawDownListTextView drawDownList_etc_khjg;
	@ViewInject(id=R.id.dzcpkh_xsz) BankEditText txt_khxsz;
	@ViewInject(id=R.id.dzcpkh_cphm) BankEditText txt_khcph;
	@ViewInject(id=R.id.dzcpkh_zjlx) BankDrawDownListTextView drawDownList_etc_khzjlx;
	@ViewInject(id=R.id.noScrollgridviewEtc, itemClick="onItemClick") GridView gridview_phone;
	@ViewInject(id=R.id.dzcpkh_khckzh) BankEditText txt_khckzh;
	@ViewInject(id=R.id.dzcpkh_khxtk) BankEditText txt_khxtk;
	@ViewInject(id=R.id.dzcpkh_sbxlh) BankEditText txt_sbxlh;
	List<Item> zjList = new ArrayList<Item>();
	List<Item> jgList = new ArrayList<Item>();
	TXtBm  mTXtBm;
	DzCpKhXxEtcPresenter   mDzCpKhXxEtcPresenter;
	DzCpKhXxFjPresenter  mDzCpKhXxFjPresenter;
	DzCpkhXxDjEtc  mDzCpkhXxDjEtc;
	TAppFjxxb  mTAppFjxxb;
	String appuuid="",fjuuid,zt;
	Long id,fjid;
	Date fjsqsj,etcsqsj;
	PhoneGridAdapter adapter;
	PopupWindows mPopupWindows;
	RegularUtil checkCard;
	MobileCheck  mobileCheck;
	String  isCheck,po;
	boolean idMoblieCheck;
	Long idKey;
	
	/** 提示框 */
    ProgressDialog mProgressDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_dzcpkhxx_etc);
		
		initView();
		init();
		
	}
	private void  initView(){
		txt_header_title.setText(getResources().getString(R.string.param131));
		Intent intent = this.getIntent();
		appuuid = intent.getStringExtra("appuuid");
		idKey = intent.getLongExtra("idKey", 0);
		po = intent.getStringExtra("po");
		/*scbs = intent.getStringExtra("scbs");
		
		if(scbs==null){
			scbs = "0";
		}*/
		photo_bt_del.setBackgroundDrawable(null);
		photo_bt_del.setText(getResources().getString(R.string.param132));
		txt_khxm.setLabel(R.string.dzcpxxdj_kh_xm);
		txt_khxm.setHint(R.string.dzcpxxdj_kh_xm_hint);
		txt_khxm.setImage(null);
		txt_khsfzhm.setLabel(R.string.dzcpxxdj_kh_sfzhm);
		txt_khsfzhm.setHint(R.string.dzcpxxdj_kh_sfzhm_hint);
		txt_khsfzhm.setImage(null);
		txt_khsjhm.setLabel(R.string.dzcpxxdj_kh_sjhm);
		txt_khsjhm.setHint(R.string.dzcpxxdj_kh_sjhm_hint);
		txt_khsjhm.setImage(null);
		drawDownList_etc_khjg.setLabel(R.string.dzcpxxdj_kh_jg);
		drawDownList_etc_khzjlx.setLabel(R.string.dzcpxxdj_kh_zjlx);
		zjList = AppUtil.getCanshuByType(this, AppUtil.SHARE_DATA_ZJLX_CJKH);
		drawDownList_etc_khzjlx.setItem(zjList);
		drawDownList_etc_khzjlx.setValue("1");
		txt_khxsz.setLabel(R.string.dzcpxxdj_kh_xsz);
		txt_khxsz.setHint(R.string.dzcpxxdj_kh_xsz_hint);
		txt_khxsz.setImage(null);
		txt_khcph.setLabel(R.string.dzcpxxdj_kh_cph);
		txt_khcph.setHint(R.string.dzcpxxdj_kh_cph_hint);
		txt_khcph.setImage(null);
		txt_khckzh.setLabel(R.string.dzcpxxdj_kh_ckzh);
		txt_khckzh.setHint(R.string.dzcpxxdj_kh_ckzh_hint);
		txt_khckzh.setImage(null);
		txt_khckzh.setVisibility(View.GONE);
		txt_khxtk.setLabel(R.string.dzcpxxdj_kh_xtk);
		txt_khxtk.setHint(R.string.dzcpxxdj_kh_xtk_hint);
		txt_khxtk.setImage(null);
		txt_khxtk.setVisibility(View.GONE);
		txt_sbxlh.setLabel(R.string.dzcpxxdj_kh_sbxlh);
		txt_sbxlh.setHint(R.string.dzcpxxdj_kh_sbxlh_hint);
		txt_sbxlh.setImage(null);
		txt_sbxlh.setVisibility(View.GONE);
//		int inputType = InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_NORMAL;
//		txt_khsfzhm.setInputType(inputType);
		mDzCpKhXxEtcPresenter =new DzCpKhXxEtcPresenter(this);
		mTXtBm  = new TXtBm();
		mProgressDialog = new ProgressDialog(this);
		mDzCpKhXxEtcPresenter.getAllBm("");
		 checkCard = new RegularUtil();
		 mobileCheck = new MobileCheck();
	}
	private void init() {
		Bimp.getInstance().reset();
		adapter = new PhoneGridAdapter(this);
		gridview_phone.setAdapter(adapter);
		
		}
	

	@Override
	public void showProgressDialog(String msg) {
		// TODO Auto-generated method stub
		mProgressDialog.setMessage(msg);
		mProgressDialog.show();
	}

	@Override
	public void hideProgressDialog() {
		// TODO Auto-generated method stub
		mProgressDialog.hide();
	}

	@Override
	public void showMessage(String msg) {
		// TODO Auto-generated method stub
		BaseToast.getInstance().ShowSystemToast(this, msg);
	}

	

	@Override
	public void updateView(int type, Object Obj) {
		// TODO Auto-generated method stub
		if (type == ServerUtil.server_save){
			showMessage(getResources().getString(R.string.param135));
		} else if (type == mDzCpKhXxEtcPresenter.loadDzCpKhXx){
			DzCpkhXxDjEtc  mDzCpkhXxEtc=(DzCpkhXxDjEtc) Obj;
			id=mDzCpkhXxEtc.getId();
			etcsqsj = mDzCpkhXxEtc.getSqSj();
			txt_khxm.setValue(mDzCpkhXxEtc.getKhMc());
			txt_khsfzhm.setValue(mDzCpkhXxEtc.getKhZjh());
			txt_khsjhm.setValue(mDzCpkhXxEtc.getKhSjhm());
			txt_khxsz.setValue(mDzCpkhXxEtc.getKhXsz());
			txt_khcph.setValue(mDzCpkhXxEtc.getCpHm());
			drawDownList_etc_khjg.setValue(mDzCpkhXxEtc.getBmbh());
			drawDownList_etc_khzjlx.setValue(mDzCpkhXxEtc.getKhZjlx().toString());
			drawDownList_etc_khzjlx.mEditText.setEnabled(false);
			List<TAppFjxxb> fjList = mDzCpkhXxEtc.getFjxxs();
			if(fjList!=null){
			for(int i=0;i<fjList.size();i++){
				TAppFjxxb tAppFjxxb = fjList.get(i);
				Bimp.getInstance().getDrr().add(tAppFjxxb);
			}
			updateGridView();
			}
		}
		
	}

	@Override
	public void updateList(List<DzCpkhXxDjEtc> mList) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateListUp(List<DzCpkhXxDjEtc> mList) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void success() {
		// TODO Auto-generated method stub
		Bimp.getInstance().reset();
		/*if(idKey!=0 && appuuid!=null ){
		Intent intent = new Intent();
		intent.putExtra("po",po);
		DzCpKhXxEtcActivity.this.setResult(RESULT_OK, intent);}*/
		finish();
	}

	@Override
	public void updateListBm(List<TXtBm> mList) {
		// TODO Auto-generated method stub
		for(int i=0;i<mList.size();i++){
			Item item= new Item();
			item.setKey(mList.get(i).getBh().toString());
			item.setValue(mList.get(i).getJc());
			jgList.add(item);
		}
		drawDownList_etc_khjg.setItem(jgList);
		
		if(appuuid!=null){
		mDzCpKhXxEtcPresenter.getDzcpkhxxetc(appuuid);
		}
	}
	public void saveClick(View arg0){
	    mDzCpkhXxDjEtc = new DzCpkhXxDjEtc();
	    mTAppFjxxb = new TAppFjxxb();
	       if(idKey!=0 && appuuid !=null){
	    	   mDzCpkhXxDjEtc.setAppUuid(appuuid);
	    	   mDzCpkhXxDjEtc.setId(idKey);
	    	   mDzCpkhXxDjEtc.setBmbh(drawDownList_etc_khjg.getkey().toString());
		    	mDzCpkhXxDjEtc.setKhZjh(txt_khsfzhm.getValue().toString());
		    	mDzCpkhXxDjEtc.setKhMc(txt_khxm.getValue().toString());
		    	mDzCpkhXxDjEtc.setKhZjlx(Integer.parseInt(drawDownList_etc_khzjlx.getkey().toString()));
		    	mDzCpkhXxDjEtc.setCpHm(txt_khcph.getValue().toString());
		    	mDzCpkhXxDjEtc.setKhXsz(txt_khxsz.getValue().toString());
		    	mDzCpkhXxDjEtc.setSbZt(0);
		    	mDzCpkhXxDjEtc.setXgzt(0);
		    	mDzCpkhXxDjEtc.setKhSjhm(txt_khsjhm.getValue().toString());
		    	/*String json = SharedPreferencesUtil.getSharePreferencesByKey(this, AppUtil.SHARE_LOGIN_TAG, AppUtil.SHARE_USER);*/
		    	TAppYh user = BankApplication.mInstance.getCurrentUser();
				mDzCpkhXxDjEtc.setSqrSfz(user.getZh());
				mDzCpkhXxDjEtc.setSqSj(new Date());
				mDzCpkhXxDjEtc.setZdlx(1);
				mDzCpkhXxDjEtc.setZdyxBz(0);
				List<TAppFjxxb> mList = Bimp.getInstance().getDrr();
				
				List<TAppFjxxb> fjxxb = new ArrayList<TAppFjxxb>();
				for (TAppFjxxb str : mList) {
					String path = str.getAppdz();
					TAppFjxxb mTAppFjxxb = new TAppFjxxb();
					mTAppFjxxb.setAppdz(str.getAppdz());
					mTAppFjxxb.setWybs(appuuid);
					mTAppFjxxb.setCjsj(new Date());
					mTAppFjxxb.setGnmk(2);
					mTAppFjxxb.setSczt(0);
					mTAppFjxxb.setWjlx(path.substring(path.lastIndexOf(".")+1, path.length() ));
					mTAppFjxxb.setSfz(user.getZh());
					mTAppFjxxb.setUuid(str.getUuid());
					mTAppFjxxb.setWjmc(path.substring(path.lastIndexOf("/")+1, path.length() ));
					mTAppFjxxb.setZdlx(1);
					fjxxb.add(mTAppFjxxb);
					}
//			   mDzCpkhXxDjEtc.getFjxxs().addAll(fjxxb);
				mDzCpkhXxDjEtc.setFjxxs(fjxxb);
				if(txt_khxm.getValue().toString().equals("") || txt_khsfzhm.getValue().toString().equals("") || txt_khsjhm.getValue().toString().equals("") || drawDownList_etc_khjg.getValue().toString().equals("") || txt_khxsz.getValue().toString().equals("") || txt_khcph.getValue().toString().equals("") || drawDownList_etc_khzjlx.getValue().toString().equals("")  ){
					Toast.makeText(this, getResources().getString(R.string.param136), Toast.LENGTH_SHORT).show();
				}
				else{
				 if(txt_khsfzhm.getValue().toString()!=null){
					 try {
					 isCheck=checkCard.IDCardValidate(txt_khsfzhm.getValue().toString());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				 }
				 idMoblieCheck =mobileCheck.isMobileNO(txt_khsjhm.getValue().toString());
				 if(isCheck.toString().trim().equals("") &&  idMoblieCheck==true && drawDownList_etc_khzjlx.getkey().toString().equals("1")){
					 mDzCpKhXxEtcPresenter.saveDzcpkhxxetc(mDzCpkhXxDjEtc);
				 }else if(idMoblieCheck==true && drawDownList_etc_khzjlx.getkey().toString().equals("2")){
					 mDzCpKhXxEtcPresenter.saveDzcpkhxxetc(mDzCpkhXxDjEtc);
				 }
				 else{
					 Toast.makeText(this, getResources().getString(R.string.param137), Toast.LENGTH_SHORT).show();
				 }
				
				}
	       }else{
	    	String appuuid = UUIDUtil.getUUID();
	    	mDzCpkhXxDjEtc.setAppUuid(appuuid);
	    	mDzCpkhXxDjEtc.setBmbh(drawDownList_etc_khjg.getkey().toString());
	    	mDzCpkhXxDjEtc.setKhZjh(txt_khsfzhm.getValue().toString());
	    	mDzCpkhXxDjEtc.setKhMc(txt_khxm.getValue().toString());
	    	mDzCpkhXxDjEtc.setKhZjlx(Integer.parseInt(drawDownList_etc_khzjlx.getkey().toString()));
	    	mDzCpkhXxDjEtc.setCpHm(txt_khcph.getValue().toString());
	    	mDzCpkhXxDjEtc.setKhXsz(txt_khxsz.getValue().toString());
	    	mDzCpkhXxDjEtc.setSbZt(0);
	    	mDzCpkhXxDjEtc.setXgzt(0);
	    	mDzCpkhXxDjEtc.setKhSjhm(txt_khsjhm.getValue().toString());
	    	/*String json = SharedPreferencesUtil.getSharePreferencesByKey(this, AppUtil.SHARE_LOGIN_TAG, AppUtil.SHARE_USER);*/
	    	TAppYh user = BankApplication.mInstance.getCurrentUser();
			mDzCpkhXxDjEtc.setSqrSfz(user.getZh());
			mDzCpkhXxDjEtc.setSqSj(new Date());
			mDzCpkhXxDjEtc.setZdlx(1);
			mDzCpkhXxDjEtc.setZdyxBz(0);
			List<TAppFjxxb> mList = Bimp.getInstance().getDrr();
			
			List<TAppFjxxb> fjxxb = new ArrayList<TAppFjxxb>();
			for (TAppFjxxb str : mList) {
				String path = str.getAppdz();
				TAppFjxxb mTAppFjxxb = new TAppFjxxb();
				mTAppFjxxb.setAppdz(str.getAppdz());
				mTAppFjxxb.setWybs(appuuid);
				mTAppFjxxb.setCjsj(new Date());
				mTAppFjxxb.setGnmk(2);
				mTAppFjxxb.setSczt(0);
				mTAppFjxxb.setWjlx(path.substring(path.lastIndexOf(".")+1, path.length() ));
				mTAppFjxxb.setSfz(user.getZh());
				mTAppFjxxb.setUuid(str.getUuid());
				mTAppFjxxb.setWjmc(path.substring(path.lastIndexOf("/")+1, path.length() ));
				mTAppFjxxb.setZdlx(1);
				fjxxb.add(mTAppFjxxb);
				}
//			   mDzCpkhXxDjEtc.getFjxxs().addAll(fjxxb);
			mDzCpkhXxDjEtc.setFjxxs(fjxxb);
			if(txt_khxm.getValue().toString().equals("") || txt_khsfzhm.getValue().toString().equals("") || txt_khsjhm.getValue().toString().equals("") || drawDownList_etc_khjg.getValue().toString().equals("") || txt_khxsz.getValue().toString().equals("") || txt_khcph.getValue().toString().equals("") || drawDownList_etc_khzjlx.getValue().toString().equals("")  ){
				Toast.makeText(this, getResources().getString(R.string.param138), Toast.LENGTH_SHORT).show();
			}
			else{
			 if(txt_khsfzhm.getValue().toString()!=null){
				 try {
				 isCheck=checkCard.IDCardValidate(txt_khsfzhm.getValue().toString());
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			 }
			 idMoblieCheck =mobileCheck.isMobileNO(txt_khsjhm.getValue().toString());
			 if(isCheck.toString().trim().equals("") &&  idMoblieCheck==true && drawDownList_etc_khzjlx.getkey().toString().equals("1")){
				 mDzCpKhXxEtcPresenter.saveDzcpkhxxetc(mDzCpkhXxDjEtc);
			 }else if(idMoblieCheck==true && drawDownList_etc_khzjlx.getkey().toString().equals("2")){
				 mDzCpKhXxEtcPresenter.saveDzcpkhxxetc(mDzCpkhXxDjEtc);
			 }
			 else{
				 Toast.makeText(this, getResources().getString(R.string.param139), Toast.LENGTH_SHORT).show();
			 }
			
			}
	       }
			/*mDzCpKhXxFjPresenter.saveDzcpkhxxfj(mTAppFjxxb);*/
}
	 public static void closeKeybord(Activity activity) {
	        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
	        if (null == imm) {
	           return;
	        }
	        if(imm.isActive()){
	            if (null != activity.getCurrentFocus() && null != activity.getCurrentFocus().getWindowToken()) {
	                imm.hideSoftInputFromWindow(activity.getCurrentFocus().getApplicationWindowToken(), 0);
	            }
	        }
	    }
	
	/**
	 * 图片选择gridview点击事件
	 * @param arg0
	 * @param arg1
	 * @param arg2
	 * @param arg3
	 */
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
		if (arg2 == Bimp.getInstance().getBmp().size()){
			closeKeybord(DzCpKhXxEtcActivity.this);
			mPopupWindows = new PopupWindows(this, gridview_phone,1);
		} else {
			Intent intent = new Intent(this, PhotoActivity.class);
			intent.putExtra("ID", arg2);
			if(id!=null){
				intent.putExtra("delete", true);
			}else{
			   intent.putExtra("delete", true);}
			this.startActivityForResult(intent, AppUtil.activity_phone_requestcode);
		}
	}
	
	
	public void updateGridView(){
		adapter.update();
	}
	
	public void setPhone(){
		if (Bimp.getInstance().getCount() < 9){
			if (mPopupWindows.savePath != null){
				/*String path = mPopupWindows.savePath;
				TAppFjxxb mTAppFjxxb = new TAppFjxxb();
				mTAppFjxxb.setAppdz(path);
				mTAppFjxxb.setSczt(-1);
				Bimp.getInstance().getDrr().add(mTAppFjxxb);
				
				updateGridView();*/
				String fileName = mPopupWindows.uuid + "." + mPopupWindows.wjlx;
				TAppFjxxb mTAppFjxxb = new TAppFjxxb();
				mTAppFjxxb.setSczt(0);
				
				mTAppFjxxb.setUuid(mPopupWindows.uuid);
				mTAppFjxxb.setWjlx(mPopupWindows.wjlx);
				mTAppFjxxb.setWjmc(fileName);
				//获取本地保存路径
				mTAppFjxxb.setAppdz(ToolUtil.getFilePath(fileName));
				Bimp.getInstance().getDrr().add(mTAppFjxxb);
				
				  updateGridView();
				
				
			}
		}
	}
	
	@Override  
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == AppUtil.activity_photo_requestcode) {
			if(resultCode == Activity.RESULT_CANCELED){
				
			}else{
				this.setPhone();
			}
			
		}
		if (requestCode == AppUtil.activity_picture_requestcode && resultCode == AppUtil.activity_picture_resultcode) {
			updateGridView();
		}
		if (requestCode == AppUtil.activity_phone_requestcode) {
			updateGridView();
		}
	}
	@Override
	public void scsuccess() {
		// TODO Auto-generated method stub
		Bimp.getInstance().reset();
		if(idKey!=0 && appuuid!=null ){
			Intent intent = new Intent();
			intent.putExtra("po",po);
			DzCpKhXxEtcActivity.this.setResult(RESULT_OK, intent);}
		finish();
	}
	@Override
	public void AlertDialog() {
		// TODO Auto-generated method stub
				new AlertDialog.Builder(this).setTitle(getResources().getString(R.string.param141)).setMessage(getResources().getString(R.string.param140)).setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub
						finish();
					}
				}).show();
			}
	
	
	
	
}
